﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
[ScriptService]
public class Service : System.Web.Services.WebService
{
    public Service () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    //[WebMethod(EnableSession = true)]
    //public string HelloWorld() {
    //    return "Hello World";
    //}

    //[WebMethod(EnableSession = true)]
    //public string getVar()
    //{
    //    return (string)System.Web.HttpContext.Current.Session["var"];
    //}

    [WebMethod(EnableSession = true)]
    public GoogleMapObject GetGoogleMapObject()
    {
        GoogleMapObject objGoogle = (GoogleMapObject)System.Web.HttpContext.Current.Session["GOOGLE_MAP_OBJECT"];
        System.Web.HttpContext.Current.Session["GOOGLE_MAP_OBJECT_OLD"] = new GoogleMapObject(objGoogle);
        return objGoogle;
    }

    [WebMethod(EnableSession = true)]
    public GoogleMapObject GetOptimizedGoogleObject()
    {
        GoogleMapObject objGoogleNew = (GoogleMapObject)System.Web.HttpContext.Current.Session["GOOGLE_MAP_OBJECT"];
        GoogleMapObject objGoogleOld = (GoogleMapObject)System.Web.HttpContext.Current.Session["GOOGLE_MAP_OBJECT_OLD"];
        GoogleMapObject objGoogle = new GoogleMapObject();

        if (objGoogleOld != null)
        {

            for (int i = 0; i < objGoogleNew.Markers.Count; i++)
            {
                string pointStatus = "";
                GooglePoint NewPoint = objGoogleNew.Markers[i];
                GooglePoint OldPoint = objGoogleOld.Markers[NewPoint.ID];
                //if old point not found, means this is a new point.
                if (OldPoint == null)
                {
                    pointStatus = "N"; //New
                }
                else
                {
                    //If old point found and old not equal to new point, means it's value is changed.
                    if (!OldPoint.Equals(NewPoint))
                    {
                        pointStatus = "C"; //Changed
                    }
                    //Remove found point from old object. This is to reduce iteration in next loop.
                    objGoogleOld.Markers.Remove(OldPoint.ID);
                }
                if (pointStatus != "")
                {
                    //If new point is changed, add it in list which is to be sent to client.
                    NewPoint.PointStatus = pointStatus;
                    objGoogle.Markers.Add(NewPoint);
                }
            }
            //Loop through rest of old points to mark them as deleted.
            for (int i = 0; i < objGoogleOld.Markers.Count; i++)
            {
                GooglePoint OldPoint = objGoogleOld.Markers[i];
                OldPoint.PointStatus = "D";
                objGoogle.Markers.Add(OldPoint);
            }

        }

        objGoogle.CenterPoint = objGoogleNew.CenterPoint;
        objGoogle.ZoomLevel = objGoogleNew.ZoomLevel;
        //Save new Google object state in session variable.
        //System.Web.HttpContext.Current.Session["GOOGLE_MAP_OBJECT_OLD"] = objGoogleNew;
        System.Web.HttpContext.Current.Session["GOOGLE_MAP_OBJECT_OLD"] = new GoogleMapObject(objGoogleNew);

        return objGoogle;
    }
}